﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using TuyenSinh.DLL;
using TuyenSinh.Ultilities;

namespace TuyenSinh.DAL
{
    public class DASchools : BaseDAL
    {
        public DataSet GetRegion()
        {
            DataSet ds = new DataSet();
            try
            {
                DbCommand cmd;
                cmd = DB.GetStoredProcCommand("sp_GetRegion");

                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("Get Region ", ex);
                return null;
            }
            return ds;
        }

        public DataSet GetSchoolType()
        {
            DataSet ds = new DataSet();
            try
            {
                DbCommand cmd;
                cmd = DB.GetStoredProcCommand("sp_GetSchoolType");

                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("Get SchoolType ", ex);
                return null;
            }
            return ds;
        }


        public DataSet GetSchoolGroup()
        {
            DataSet ds = new DataSet();
            try
            {
                DbCommand cmd;
                cmd = DB.GetStoredProcCommand("sp_GetSchoolGroup");

                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("Get School Group ", ex);
                return null;
            }
            return ds;
        }

        public bool InsertSChool(string sName, int iRegionId, string sDesc, string sAddress, int sSchoolTypeId, int sSchoolGroupId, int isActive, int isDelete)
        {

            bool bResult = false;
            DbCommand cmd;

            cmd = DB.GetStoredProcCommand("sp_InsertSchool");
            DB.AddInParameter(cmd, "@Name", SqlDbType.NVarChar, sName);
            DB.AddInParameter(cmd, "@RegionId", SqlDbType.Int, iRegionId);
            DB.AddInParameter(cmd, "@Desc", SqlDbType.NVarChar, sDesc);
            DB.AddInParameter(cmd, "@Address", SqlDbType.NVarChar, sAddress);
            DB.AddInParameter(cmd, "@SchoolTypeId", SqlDbType.Int, sSchoolTypeId);
            DB.AddInParameter(cmd, "@SchoolGroupId", SqlDbType.Int, sSchoolGroupId);

            DB.AddInParameter(cmd, "@IsActive", SqlDbType.Bit, isActive == 1);
            DB.AddInParameter(cmd, "@IsDelete", SqlDbType.Bit, isDelete == 1);

            try
            {
                DB.ExecuteNonQuery(cmd);
                bResult = true;
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("System Error - Insert school", ex);
                bResult = false;
            }
            return bResult;
        }

        public DataSet GetSchool(string sName, int iRegion, int iShoolType, int iSchoolGroup, int isActive, int isDelete)
        {
            //

            DataSet ds = new DataSet();
            DbCommand cmd;

            cmd = DB.GetStoredProcCommand("sp_GetSchoolFilter");
            if (!string.IsNullOrEmpty(sName))
            {
                DB.AddInParameter(cmd, "@Name", SqlDbType.NVarChar, sName);
            }
            if (iRegion != 0)
            {
                DB.AddInParameter(cmd, "@RegionId", SqlDbType.Int, iRegion);
            }
            if (iShoolType != 0)
            {
                DB.AddInParameter(cmd, "@SchoolTypeId", SqlDbType.Int, iShoolType);
            }
            if (iSchoolGroup != 0)
            {
                DB.AddInParameter(cmd, "@SchoolGroupId", SqlDbType.Int, iSchoolGroup);
            }
            DB.AddInParameter(cmd, "@IsActive", SqlDbType.Bit, isActive == 1);
            DB.AddInParameter(cmd, "@IsDelete", SqlDbType.Bit, isDelete == 1);

            try
            {
                ds = DB.ExecuteDataSet(cmd);
            }
            catch (Exception ex)
            {
                Logger.FrameworkLogger.Error("System Error - Get school", ex);
                ds = null;
            }
            return ds;
        }
    }
}
